﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using RookieLibSharp.Configuration;
using System.IO;
using System.Diagnostics;
namespace BirthdayRemainder
{
    public partial class FrmSet : Form
    {
        /// <summary>
        /// 是否需要刷新列表
        /// </summary>
        public bool NeedRefreshList { get; protected set; }
        /// <summary>
        /// 是否需要修改背景颜色
        /// </summary>
        public bool NeedRefreshBackColor { get; protected set; }

        public FrmSet()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 开机是否启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbRunPowerOn_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckb = (CheckBox)sender;
            Funcs.runWhenStart(ckb.Checked, Application.ExecutablePath);
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_RUN_POWER_ON, ckb.Checked);
        }
        /// <summary>
        /// 加载配置
        /// </summary>
        private void LoadCfg()
        {
            bool bVal;
            this.ckbRunPowerOn.Checked = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_RUN_POWER_ON);//开机启动本软件

            string[] ArrName = SftMgr.DbGetAllName();
            if (ArrName == null)
            {
                this.btnDeleteAllRecord.Enabled = false;//如果数据库中没有数据，则将删除全部数据按钮disable
            }
            int intVal = Cfg.ReadInt(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REMAIND_DAYS);//提醒天数
            if ((intVal>=0) && (intVal<cbbRemaindDays.Items.Count))
            {
                cbbRemaindDays.SelectedIndex = intVal;
            }

            intVal = Cfg.ReadInt(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_LST_VIEW_BACK_COLOR);
            LabBackColor.BackColor = Color.FromArgb(intVal ==0?GlobalValue.SFT_CFG_LST_VIEW_BACK_COLOR_DEFAULT:intVal);//背景颜色
            
            ckbCloseFrmAfterAdd.Checked = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_CLOSE_FORM_AFTER_ADD);//添加后是否关闭添加窗口

            this.ckbFrmRemind.Checked = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_FRM_REMIND, true);//弹窗提醒

            bVal = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_BR, true);
            this.ckbBR.Checked = !bVal;//是否运行后台服务
            this.ckbBR.Checked = bVal;

            this.ckbMailRemind.Checked = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND,false);//邮件提醒
            this.grupMainRemind.Enabled = ckbMailRemind.Checked;

            this.tbSendMailAddr.Text = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_SEND_MAIL_ADDR);
            this.tbSendMailPassword.Text = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_SEND_MAIL_PSD);
            this.tbReceiveMailAddr.Text = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_RECEIVE_MAIL_ADDR);

            

            intVal = Cfg.ReadInt(GlobalValue.SFT_CFG,GlobalValue.SFT_CFG_REMAIND_INTERVAL,5);
            if (this.cbbFrmInterval.Items.Contains(intVal.ToString()))
            {
                this.cbbFrmInterval.Text = intVal.ToString();
            }
            else
            {
                this.cbbFrmInterval.SelectedIndex = 2;
                Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REMAIND_INTERVAL, this.cbbFrmInterval.Text);
            }

            intVal = Cfg.ReadInt(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_SET_PAGE_INDEX, 0);
            if (intVal>=0 && intVal<this.tabControl.TabPages.Count)
            {
                this.tabControl.SelectedIndex = intVal;
            }
        }
        private void SaveCfg()
        {
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REMAIND_DAYS, cbbRemaindDays.SelectedIndex);

            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_SEND_MAIL_ADDR, this.tbSendMailAddr.Text);
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_SEND_MAIL_PSD, tbSendMailPassword.Text);
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_RECEIVE_MAIL_ADDR, tbReceiveMailAddr.Text);
            
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_FRM_REMIND, ckbFrmRemind.Checked);
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_BR, this.ckbBR.Checked);
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_SET_PAGE_INDEX, this.tabControl.SelectedIndex);
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REMAIND_INTERVAL, this.cbbFrmInterval.Text);
            SetExRun();
        }
        private void FrmSet_Load(object sender, EventArgs e)
        {
            LoadCfg();
            this.Icon = BirthdayRemainder.Properties.Resources.ico_main;
            NeedRefreshList = false;
            NeedRefreshBackColor = false;
        }
        /// <summary>
        /// 删除全部记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteAllRecord_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除全部记录？","提醒",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                SftMgr.DbDeleteAllRecord();
                MessageBox.Show("删除成功", "提醒", MessageBoxButtons.OK);
                this.btnDeleteAllRecord.Enabled = false;
                this.NeedRefreshList = true;
            }
        }

        private void LabBackColor_DoubleClick(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                Color c = LabBackColor.BackColor;
                if (colorDlg.Color != LabBackColor.BackColor)
                {
                    NeedRefreshBackColor = true;
                    LabBackColor.BackColor = colorDlg.Color;
                    Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_LST_VIEW_BACK_COLOR, LabBackColor.BackColor.ToArgb());
                }
            }
        }

        private void ckbCloseFrmAfterAdd_CheckedChanged(object sender, EventArgs e)
        {
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_CLOSE_FORM_AFTER_ADD, ckbCloseFrmAfterAdd.Checked);
        }

        private void ckbMailRemind_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckb = (CheckBox)sender;
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND, ckb.Checked);
            this.grupMainRemind.Enabled = ckb.Checked;
        }
        private bool ValidCheck()
        {
            if (this.ckbMailRemind.Checked && this.ckbBR.Checked)
            {
                if (!Funcs.CheckMailAddr(tbSendMailAddr.Text))
                {
                    MessageBox.Show("发送邮件地址不合法");
                    return false;
                }
                if (tbSendMailPassword.Text.Trim().Length == 0)
                {
                    MessageBox.Show("发送邮箱密码为空");
                    return false;
                }
                if (!Funcs.CheckMailAddr(tbReceiveMailAddr.Text))
                {
                    MessageBox.Show("接受邮件地址不合法");
                    return false;
                }
            }

            return true;
        }

        private void FrmSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!ValidCheck())
            {
                e.Cancel = true;
            }
            SaveCfg();
        }
        private void SetExRun()
        {
            CheckBox ckb = this.ckbBR;
            string BRExtention = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + GlobalValue.EXTENTION_APP;
            Funcs.runWhenStart(ckb.Checked, BRExtention);
            if (ckb.Checked)
            {
                Funcs.RunApp(BRExtention,GlobalValue.EXTENTION_APP);
            }
            else
            {
                Funcs.KillApp(Path.GetFileNameWithoutExtension(BRExtention));
            }
        }

       
        private void ckbBR_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckb = (CheckBox)sender;
            this.cbbRemaindDays.Enabled = ckb.Checked;
            this.ckbMailRemind.Enabled = ckb.Checked;
            this.ckbFrmRemind.Enabled = ckb.Checked;
            this.grupMainRemind.Enabled = ckb.Checked;
            this.tbReceiveMailAddr.Enabled = ckb.Checked;
            this.tbSendMailAddr.Enabled = ckb.Checked;
            this.tbSendMailPassword.Enabled = ckb.Checked;
            this.cbbFrmInterval.Enabled = ckb.Checked && this.ckbFrmRemind.Checked;
        }

        private void ckbFrmRemind_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckb = (CheckBox)sender;

            this.cbbFrmInterval.Enabled = ckb.Checked;
        }

    }
}
